package com.dhcc.sds.base.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.annotion.CacheClear;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.base.cache.formitemdic.IFormItemDicCache;
import com.dhcc.sds.base.entity.SdsQcfitemDicEntity;
import com.dhcc.sds.base.service.ISdsQcfitemDicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 表单值域字典表
 * 
 * @ClassName: SdsQcfitemDicController
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:10:47
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsqcfitemdic")
public class SdsQcfitemDicController extends BaseController {

	private String PREFIX = "/backend/sds/sdsqcfitemdic/";

	@Autowired
	private ISdsQcfitemDicService sdsQcfitemDicService;
	

    /**
     * 跳转到表单值域字典表列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "sdsqcfitemdic.html";
    }
	
    /**
     * 跳转到新增表单值域字典表
     */
    @RequestMapping("/to_add")
    public String toAdd() {
        return PREFIX + "sdsqcfitemdic_add.html";
    }
	
	/**
     * 跳转到修改表单值域字典表
     */
    @RequestMapping("/to_update/{sdsQcfitemDicId}")
    public String toUpdate(@PathVariable Long sdsQcfitemDicId, Model model) {
        SdsQcfitemDicEntity sdsQcfitemDic = this.sdsQcfitemDicService.selectById(sdsQcfitemDicId);
        model.addAttribute("sdsQcfitemDic",sdsQcfitemDic);
        return PREFIX + "sdsqcfitemdic_edit.html";
    }

    /**
     * 获取表单值域字典表列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<SdsQcfitemDicEntity> page = new PageFactory<SdsQcfitemDicEntity>().dataGridPage();
        List<SdsQcfitemDicEntity> result = this.sdsQcfitemDicService.page(page,params);
        page.setRecords((List<SdsQcfitemDicEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增表单值域字典表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @CacheClear(IFormItemDicCache.CACHE_NAME)
    public Object add(SdsQcfitemDicEntity sdsQcfitemDic) throws UnsupportedEncodingException {
        if (CommonUtil.isOneEmpty(sdsQcfitemDic)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        String matchRule = java.net.URLDecoder.decode(sdsQcfitemDic.getMatchRule(), "UTF-8");
        sdsQcfitemDic.setMatchRule(matchRule);
        sdsQcfitemDic.setUpdateUser("admin");
        sdsQcfitemDic.setUpdateDate(new Date());
        sdsQcfitemDic.insert();
        return R.ok();
    }

    /**
     * 删除表单值域字典表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @CacheClear(IFormItemDicCache.CACHE_NAME)
    public Object delete(@RequestParam Long sdsQcfitemDicId) {
	
        this.sdsQcfitemDicService.deleteById(sdsQcfitemDicId);

        return R.ok();
    }

    /**
     * 修改表单值域字典表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @CacheClear(IFormItemDicCache.CACHE_NAME)
    public Object update(SdsQcfitemDicEntity sdsQcfitemDic) throws UnsupportedEncodingException {
        if (CommonUtil.isOneEmpty(sdsQcfitemDic)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsQcfitemDicEntity old = this.sdsQcfitemDicService.selectById(sdsQcfitemDic.getId());
        String matchRule = URLDecoder.decode(sdsQcfitemDic.getMatchRule(), "UTF-8");

        old.setFormItemId(sdsQcfitemDic.getFormItemId());
        old.setCode(sdsQcfitemDic.getCode());
        old.setDesc(sdsQcfitemDic.getDesc());
        old.setIndNo(sdsQcfitemDic.getIndNo());
        old.setKeyword(sdsQcfitemDic.getKeyword());
        //old.setGroup(sdsQcfitemDic.getGroup());
        //old.setResume(sdsQcfitemDic.getResume());
        old.setMatchRule(matchRule);
        old.setIsActive(sdsQcfitemDic.getIsActive());
        old.setIsDefault(sdsQcfitemDic.getIsDefault());
        old.setUpdateUser("admin");
        old.setUpdateDate(new Date());
        old.updateAllColumnById();
        return R.ok();
    }

    /**
     * 修改表单项目值域字典为无效
     */
    @RequestMapping(value = "/setInvalidStatus")
    @ResponseBody
    @CacheClear(IFormItemDicCache.CACHE_NAME)
    public Object setInvalidStatus(@RequestParam Long sdsQcfitemDicId) {
        SdsQcfitemDicEntity entity = new SdsQcfitemDicEntity();
        entity.setId(sdsQcfitemDicId);
        entity.setIsActive(0);
        this.sdsQcfitemDicService.updateById(entity);
        return R.ok();
    }

    /**
     * 获取表单项目值域字典的tree列表
     */
    @RequestMapping(value = "/tree/{formItemId}")
    @ResponseBody
    public List<ZTreeNode> itemDicTree(@PathVariable Long formItemId) {
        return sdsQcfitemDicService.getItemDicTree(formItemId);
    }

    /**
     * 获取表单项目值域字典集合
     */
    @RequestMapping(value = "/itemDics/{formItemId}")
    @ResponseBody
    public List<SdsQcfitemDicEntity> itemDics(@PathVariable Long formItemId) {
        Map param = new HashMap();
        param.put("formItemId", formItemId);
        param.put("isActive", 1);
        return sdsQcfitemDicService.list(param);
    }
	
}
